<?php

namespace App\Http\Controllers\admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CompanyOrder\CompanyOrderRequest;
use App\Models\Company\CompanyContract;
use App\Models\Company\MyOrder;
use Illuminate\Http\Request;
use Style,DB,View;

class CompanyOrderController extends Controller
{
    /**
     * 定单列表
     * @return mixed
     */
    public function index(Request $request){
        $input = arrayFilter($request->only('keyword'));
        $keyword = (!empty($input['keyword'])) ? $input['keyword'] : '';
        //定单信息与支付方式
        $MyOrder = MyOrder::with(['myOrderWithCollOptions' => function($query){
            $query->select('opt_id','opt_name')
                ->where('sign' , 'pay');
            }])
            ->with(['myOrderWithCompanyUser' =>  function($query) {
                $query->select('uid','username','company_name');
            }])
            ->orderBy('updated_at','desc');

        if (!empty($keyword)) {
            $MyOrder->whereHas('myOrderWithCompanyUser', function ($q) use($keyword) {
                $q->where('username','like','%'.$keyword.'%')
                ->orWhere('company_name','like','%'.$keyword.'%');
            });
        }
        $MyOrder = $MyOrder->paginate(10);

        if($MyOrder){
            foreach($MyOrder as $v){
                switch($v->stats){
                    case 0:
                        $v->payStats = '未支付';
                        break;
                    case 1:
                        $v->payStats = '已取消';
                        break;
                    case 2:
                        $v->payStats = '已支付';
                        break;
                }
            }
        }

       return  Style::view('admin.company_manage.order.list',compact('MyOrder'));

    }

    /**
     * 修改定单
     * @param $orderId
     * @return mixed
     */
    public function edit($orderId){

        $MyOrder = MyOrder::where('id',$orderId)->first();
        return Style::view('admin.company_manage.order.edit',compact('MyOrder'));
    }

    /**
     * 更新定单 金额
     * @param $orderId
     * @param CompanyOrderRequest $request
     */
    public function update($orderId,CompanyOrderRequest $request){

        $orderId = intval($orderId);
        $money = number_format($request->money,2,'.','');

        if(MyOrder::where('id',$orderId)->update(['money' => $money,'stats' => intval($request->stats)])){
            $msg = "订单修改成功";
            $status = 0;
        }else{
            $msg = "操作失败";
            $status = 1;
        }

        return redirect(route("admin.company_order.index"))->with(['status' => $status,'msg' => $msg]);
    }

    /**
     * 删除定单
     * @param $orderId
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($orderId){

        $orderId = intval($orderId);

        $MyOrder = MyOrder::with('myOrderWithVipPackage')
            ->with(['myOrderWithCompanyUser' => function($query){
                $query->select('uid','viptime','vip_resume_number','vip_integral','vip_post_number','resume_number','integral');
            }])
            ->with(['myOrderWithContract' => function($query) use($orderId) {
                $query->select('uid','start_time','end_time')->where('order_id',$orderId)->orderBy('end_time','desc');
            }])
            ->where('id',$orderId)->first();

        // *删除规则
        // 删除定单的同时，删除合同和减去企业会员 vip 时间，简历数，积分和发布岗位
        // 每次购买套餐。vip 信息数据是可以叠加的，vip时间以最后一次合同为准
        // 如果合同过期，则不操作
        if($MyOrder){
            if(!empty($MyOrder->myOrderWithContract)){
                $currentTime = strtotime(date("Y-m-d H:i:s"));//当有时间
                $contractTime = strtotime($MyOrder->myOrderWithContract->end_time);//本次合同结束时间
                if(!empty($MyOrder->myOrderWithCompanyUser)){
                    $vipTime = strtotime($MyOrder->myOrderWithCompanyUser->viptime);//企业用户vip时间

                    if(($contractTime-$currentTime) > 0 && ($contractTime-$vipTime) >= 0){
                        //vip time
                        $MyOrder->myOrderWithCompanyUser->viptime = $MyOrder->myOrderWithContract->start_time;
                        //vip简历数
                        $vip_resume_number = ($MyOrder->myOrderWithCompanyUser->vip_resume_number - $MyOrder->myOrderWithVipPackage->vip_resume_number);
                        $MyOrder->myOrderWithCompanyUser->vip_resume_number = $vip_resume_number < 1 ? $vip_resume_number = 0 : $vip_resume_number;
                        //vip积分
                        $vip_integral = ($MyOrder->myOrderWithCompanyUser->vip_integral - $MyOrder->myOrderWithVipPackage->vip_integral);
                        $MyOrder->myOrderWithCompanyUser->vip_integral = $vip_integral < 1 ? $vip_integral = 0 : $vip_integral;
                        //vip 职位数
                        $vip_post_number = ($MyOrder->myOrderWithCompanyUser->vip_post_number - $MyOrder->myOrderWithVipPackage->vip_post_number);
                        $MyOrder->myOrderWithCompanyUser->vip_post_number = $vip_post_number < 1 ? $vip_post_number = 0 : $vip_post_number;
                        //非会员简历数
                        $resume_number = ($MyOrder->myOrderWithCompanyUser->resume_number - $MyOrder->myOrderWithVipPackage->resume_number);
                        $MyOrder->myOrderWithCompanyUser->resume_number = $resume_number < 1 ? $resume_number = 0 : $resume_number;
                        //非会员积分
                        $integral = ($MyOrder->myOrderWithCompanyUser->integral - $MyOrder->myOrderWithVipPackage->integral);
                        $MyOrder->myOrderWithCompanyUser->integral = $integral < 1 ? $integral = 0 : $integral;
                        //更新
                        $MyOrder->myOrderWithCompanyUser->save();
                    }
                }

                //删除合同
                CompanyContract::where('order_id',$orderId)->delete();//同时删除关联订单
            }
            //删除定单
            MyOrder::destroy($orderId);
            $msg = "删除成功";
            $status = 0;
        }else{
            $msg = "操作失败";
            $status = 1;
        }

        return response()->json(['status' => $status,'msg' => $msg]);
    }
}
